<?php
require_once 'init_app.php';

set_time_limit(0); 

DB::query(NULL, "TRUNCATE TABLE `available_products`")->execute();

// Одежда - для женщин, мужчин и детей
/*$url = 'http://export.admitad.com/ru/webmaster/websites/121512/products/export/?user=Sianukvil&code=bc752de506&region=RU&price_from=&price_to=&category=312&category=1702&category=1848&keyword=&format=xml';
$n = Model_ProductParser_Admitad::instance()->parse($url);
echo 'General - '.$n."\n"; 
echo "---\n";      */

// Wildberries
$url = 'http://export.admitad.com/ru/webmaster/websites/121512/products/export_adv_products/?user=Sianukvil&code=bc752de506&advcampaign_id=4102&format=xml';
$shop = ORM::factory('shop', 4102);
$n = Model_ProductParser_Admitad::instance()->parse($url, $shop);
echo 'Wildberries - '.$n."\n";    
echo "---\n";

// Lamoda
$url = 'http://export.admitad.com/ru/webmaster/websites/121512/products/export_adv_products/?user=Sianukvil&code=bc752de506&advcampaign_id=1001&format=xml';
$shop = ORM::factory('shop', 1001);
$n = Model_ProductParser_Admitad::instance()->parse($url, $shop);
echo 'Lamoda - '.$n."\n"; 
echo "---\n";

// SHOWROOMS
$url = 'http://export.admitad.com/ru/webmaster/websites/121512/products/export_adv_products/?user=Sianukvil&code=bc752de506&advcampaign_id=3761&format=xml';
$shop = ORM::factory('shop', 3761);
$n = Model_ProductParser_Admitad::instance()->parse($url, $shop);
echo 'SHOWROOMS - '.$n."\n";
echo "---\n"; 

// Halens
$url = 'http://export.admitad.com/ru/webmaster/websites/121512/products/export_adv_products/?user=Sianukvil&code=bc752de506&advcampaign_id=743&format=xml';
$shop = ORM::factory('shop', 743);
$n = Model_ProductParser_Admitad::instance()->parse($url, $shop);
echo 'Halens - '.$n."\n";
echo "---\n"; 

// YOOX
/*$url = 'http://export.admitad.com/ru/webmaster/websites/121512/products/export_adv_products/?user=Sianukvil&code=bc752de506&advcampaign_id=5057&format=xml';
$shop = ORM::factory('shop', 5057);
$n = Model_ProductParser_Admitad::instance()->parse($url, $shop);
echo 'YOOX - '.$n."\n";
echo "---\n";    */

// Stylepit
$url = 'http://export.admitad.com/ru/webmaster/websites/121512/products/export_adv_products/?user=Sianukvil&code=bc752de506&advcampaign_id=5533&format=xml'; 
$shop = ORM::factory('shop', 5533);
$n = Model_ProductParser_Admitad::instance()->parse($url, $shop);
echo 'Stylepit - '.$n."\n";
echo "---\n";
 

// Теперь придется парсить все, потому что если отключить адмитадовские товары, то они пометятся недоступными
DB::query(Database::UPDATE, "UPDATE products AS p SET available = 1
                             WHERE EXISTS (
                                SELECT * FROM available_products
                                WHERE original_id = p.original_id
                                  AND shop_id = p.shop_id                                
                             )")->execute();
DB::query(Database::UPDATE, "UPDATE products AS p SET available = 0
                             WHERE NOT EXISTS (
                                SELECT * FROM available_products
                                WHERE original_id = p.original_id
                                  AND shop_id = p.shop_id                                
                             )")->execute();
echo "ok\n";